package com.xianzhiOA.controller;

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzProject;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * ProjectController
 *
 * @author 甘汝雷
 * @create 2017-04-19 10:17
 **/
@Controller
@RequestMapping("/Project")
public class XzProjectController {
    @Autowired
    private XzProjectService projectService;
    @Autowired
    private XzEmpService empService;

    //添加项目
    @RequestMapping("addProject.emp")
    public ModelAndView addProject(@Validated({XzProject.F1.class}) XzProject project, BindingResult result) {
        ModelAndView mv = new ModelAndView("redirect:selProjectAll.emp");
        Map errors = new HashMap();
        if (project.getProjectName()!=null&&project.getProjectName()!=""){
            XzProject project1 = projectService.selByName(project.getProjectName());
            if (project1!=null)errors.put("projectName","项目名已存在");
        }else errors.put("projectName","项目名不能为空");
        if (!errors.isEmpty()){
            mv.addObject("errors",errors);
            mv.addObject("project",project);
            mv.addObject("empList",empService.selAll(new XzEmp()));
            mv.setViewName("backEnd/projectEdit");
            return mv;
        }
        projectService.insert(project);
        return mv;
    }

    //查询项目
    @RequestMapping("selProjectAll.emp")
    public ModelAndView selProjectAll(XzProject project, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "8") int rows,
                                      String begin, String end) {
        ModelAndView mv = new ModelAndView("backEnd/projectShow");
        PageBean<XzProject> pageBean = projectService.selAll(project, page, rows,begin,end);
        mv.addObject("projectList", pageBean.getList());
        mv.addObject("project", project);
        mv.addObject("begin", begin);
        mv.addObject("end", end);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getPageSize());
        mv.addObject("total", pageBean.getTotal());
        return mv;
    }

    //编辑项目
    @RequestMapping("projectEdit.emp")
    public ModelAndView ProjectEdit(long projectId) {
        ModelAndView mv = new ModelAndView("backEnd/projectEdit");
        XzProject pj;
        if (projectId != 0){
            pj = projectService.selById(projectId);
        }else pj = new XzProject();
        mv.addObject("project", pj);
        mv.addObject("empList",empService.selAll(new XzEmp()));
        return mv;
    }

    //修改项目
    @RequestMapping("updateProject.emp")
    public ModelAndView updateProject(XzProject project) {
        ModelAndView mv = new ModelAndView("redirect:selProjectAll.emp");
        if (project.getProjectName().equals("")){
            Map  errors = new HashMap();
            errors.put("projectName","项目名不能为空");
            mv.addObject("errors",errors);
            mv.addObject("project",project);
            mv.addObject("empList",empService.selAll(new XzEmp()));
            mv.setViewName("backEnd/projectEdit");
            return mv;
        }
        projectService.update(project);
        return mv;
    }

    //删除项目
    @RequestMapping("deleteProject.emp")
    public ModelAndView deleteProject(Long projectId) {
        ModelAndView mv = new ModelAndView("redirect:selProjectAll.emp");
        projectService.isdelete(projectId);
        return mv;
    }
}
